package com.goverse.mancraft.man.action;

import android.util.Log;

import com.goverse.mancraft.CommonParams;
import com.goverse.mancraft.world.Location;

/**
 * Created by gaoyu on 2017/4/20.
 */

public class WalkAction extends AbstractAction {

    private Location mDestination = null;

    private Runnable mWalkThread = new Runnable() {
        @Override
        public void run() {
            mHandler.postDelayed(this, 1000);
            walk();
        }
    };

    public Location getDestination() {
        return mDestination;
    }

    public void setDestination(Location destination) {
        this.mDestination = destination;
    }

    @Override
    public int getLoseHp() {
        return 2;
    }

    @Override
    public void onStop() {

        mHandler.removeCallbacks(mWalkThread);
    }

    @Override
    public void onStart() {

        Log.d("ManCraft", mMan.getName() + "-----WALKING----START---");
        Log.d("ManCraft", mMan.getName() + "-----DESTINATION----(" + getDestination().getX() + "," + getDestination().getY() + ")");

        mHandler.postDelayed(mWalkThread, 1000);
    }

    private void walk() {

        Location location = mMan.getmCurrLocation();
        if (location.getX() < getDestination().getX()) {
            location.setX(location.getX() + mMan.getVelocity());
        } else if (location.getX() > getDestination().getX()) {
            location.setX(location.getX() - mMan.getVelocity());
        } else if (location.getX() == getDestination().getX() && location.getY() != getDestination().getY()) {
            if (location.getY() < getDestination().getY()) {
                location.setY(location.getY() + mMan.getVelocity());
            } else if (location.getY() > getDestination().getY()) {
                location.setY(location.getY() - mMan.getVelocity());
            }
        } else if (location.getX() == getDestination().getX() && location.getY() == location.getY()) {
            mHandler.removeCallbacks(mWalkThread);
            if (location.equals(CommonParams.HOME)) {
                Log.d("ManCraft", mMan.getName() + "-----ARRIVED----HOME-----START-----SleepAction");
                doAction(new SleepAction());
            } else if (location.equals(CommonParams.MINE)) {
                Log.d("ManCraft", mMan.getName() + "-----ARRIVED----MINE-----START-----WorkAction");
                doAction(new WorkAction());
            } else if (location.equals(CommonParams.SHOP)) {
                Log.d("ManCraft", mMan.getName() + "-----ARRIVED----SHOP-----START-----ShopAction");
                doAction(new ShopAction());
            }
        }
    }
}
